package com.elangzhi.ssm.modules.goods.controller;

import com.elangzhi.generator.util.GenUtil;
import com.elangzhi.ssm.controller.AdminBaseController;
import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.controller.util.ParamMap;
import com.elangzhi.ssm.model.Goods;
import com.elangzhi.ssm.model.Image;
import com.elangzhi.ssm.model.Shop;
import com.elangzhi.ssm.model.User;
import com.elangzhi.ssm.modules.goods.services.GoodsService;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.shop.services.ShopService;
import com.elangzhi.ssm.modules.type.services.TypeService;
import com.elangzhi.ssm.tools.Const;
import com.elangzhi.ssm.tools.UUIDFactory;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
* 商品 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/goods")
public class GoodsController extends AdminBaseController<Goods> {

    @Override
    public ModelAndView add(ModelMap model, Goods goods) {
        model.put("typeList",typeService.listTop());
        return super.add(model, goods);
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public Tip uploadImg( @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {

        if (file != null) {
            try {
                String fileName = file.getOriginalFilename();
                String saveName = UUIDFactory.getLongId() + fileName.substring(fileName.lastIndexOf("."));
                String filePath = "/images/goods/" + saveName;
                String realPath = request.getServletContext().getRealPath(filePath);
                file.transferTo(new File(realPath));
                return new Tip(filePath);
            } catch (IOException e) {
                e.printStackTrace();
                return new Tip(2);
            }
        } else {
            return new Tip(1);
        }
    }

    /**
     * 上传商品
     * @param obj
     * @param goodsImages
     * @return
     * @throws Exception
     */
    @RequestMapping("saveGoods")
    @ResponseBody
    public Tip saveGoods(Goods obj,String goodsImages) throws Exception {
        obj.setSetTime(new Date());
        saveImages(obj.getId(),goodsImages);
        return super.save(obj);
    }


    /**
     * 添加到图片
     * @param goodsId
     * @param goodsImages
     */
    private void saveImages(Long goodsId,String goodsImages) {
        if(goodsImages != null){
            for(String url : goodsImages.split(",")){
                Image image = new Image();
                image.setId(UUIDFactory.getLongId());
                image.setInfo(goodsId);
                image.setType(2);
                image.setUrl(url);
                try {
                    imageService.insert(image);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 修改商品
     * @param goods
     * @param goodsImages
     * @return
     * @throws Exception
     */
    @RequestMapping("editGoods")
    @ResponseBody
    public Tip editGoods(Goods goods,String goodsImages) throws Exception {
        saveImages(goods.getId(),goodsImages);
        return super.update(goods);
    }

    @Override
    public ModelAndView edit(ModelMap model, @RequestParam Long id, Goods goods) throws Exception {
        Goods obj = goodsService.selectById(id);
        obj.setBannerList(imageService.listByTypeInfo(2,obj.getId()));
        model.put("data",obj);
        model.put("typeList",typeService.listTop());
        return new ModelAndView("admin/goods/edit",model);
    }


    /**
     * 获取我的商品列表 分页
     * 默认第一页，每页指定行数
     * @param request 请求参数
     * @return 实体列表
     * @throws Exception 获取实体列表失败
     */
    @RequestMapping(value="/myList")
    public ModelAndView myList(HttpServletRequest request, HttpSession session,Integer page,Integer size) throws Exception {
        if(page == null || page == 0){
            page = 1;
            size = 10;
        }
        ParamMap paramMap = new ParamMap(request);
        paramMap.put("shopId",findShopId(session));
        PageInfo<Goods> pageInfo =  goodsService.list(paramMap,page,size);
        paramMap.put("pageInfo",pageInfo);
        return new ModelAndView("admin/goods/list",paramMap);
    }

    /**
     * session中获取店铺信息
     * @return 店铺id
     */
    private Long findShopId(HttpSession session){
        try {
            Shop shop = (Shop) session.getAttribute(Const.SHOP);
            if(shop == null) return null;
            return shop.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //---------------------------- property -------------------------------

    @Resource
    private GoodsService goodsService;


    @Resource
    private ShopService shopService;

    @Resource
    private ImageService imageService;

    @Resource
    private TypeService typeService;

}
